package com.cedarhd.finance.msg.controller.api;

import com.cedarhd.finance.msg.common.JsonResult;
import com.cedarhd.finance.msg.common.ProcessAPI;
import com.cedarhd.finance.msg.common.enums.ImageTypeEnum;
import com.cedarhd.finance.msg.common.enums.ResultCode;
import com.cedarhd.finance.msg.common.utils.PicUtil;
import com.cedarhd.finance.msg.controller.Routes;
import com.cedarhd.finance.msg.domain.service.PictureService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @author lipei
 * @date 2018/3/7
 */
@Api(value = "七牛上传文件", description = "七牛上传文件")
@Controller
public class PictureController {

    private Logger logger = LoggerFactory.getLogger(PictureController.class);

    @Autowired
    private PictureService pictureService;

    @Value("${qn.url}")
    private String picUrl;

    @ApiOperation(value = "七牛上传文件", notes = "七牛上传文件", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = Routes.QINIU_UPLOAD_FILE, method = RequestMethod.POST)
    @ResponseBody
    public JsonResult upload(HttpServletRequest request,@RequestParam(value="file")MultipartFile file){
        ProcessAPI api = (result, validate) -> {
            validate.check("file",file);
            String picName = file.getOriginalFilename();
            logger.info("upload picName = {}",picName);

            if(picName.endsWith(ImageTypeEnum.JPEG.getName()) == false){
                result.setMsg("文件名需以.jpg结尾,此次上传失败");
                result.setResultCode(ResultCode.FAILED.getCode());
                return;
            }

            //1,176,301字节 = 1.12MB
            if(file.getSize() > 1176300 ){
                result.setMsg("文件太大,不能超过1MB,此次上传失败");
                result.setResultCode(ResultCode.FAILED.getCode());
                return;
            }

            if(picName.length() <= 6){
                result.setResultCode(ResultCode.FAILED.getCode());
                result.setMsg("文件名太短，此次上传失败");
                return;
            }

            PicUtil.clearPicFolder();
            picName = System.currentTimeMillis() + ".jpg";
            Boolean uploadResult = pictureService.uploadFile(file,picName);
            if(uploadResult){
                result.setResult(picUrl+"/"+picName);
            }else {
                result.setMsg(ResultCode.FAILED.getMsg());
                result.setResultCode(ResultCode.FAILED.getCode());
            }
            PicUtil.clearPicFolder();

        };
        return api.process(request);

    }
}
